package com.kvest.map.mapview.engine.utils;

import android.graphics.Bitmap;
import android.opengl.GLUtils;
import android.util.Log;

import javax.microedition.khronos.opengles.GL10;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 04.05.13
 * Time: 16:17
 * To change this template use File | Settings | File Templates.
 */
public class Utils {
    private static int[] texturesArray = new int[1];

    public static void unregisterTexture(GL10 gl, int id) {
        synchronized (Utils.class) {
            texturesArray[0] = id;
            gl.glDeleteTextures(texturesArray.length, texturesArray, 0);
        }
    }

    public static int registerTexture(GL10 gl, Bitmap bitmap) {
        int result = 0;
        synchronized (Utils.class) {
            texturesArray[0] = 0;

            // generate one texture pointer
            gl.glGenTextures(1, texturesArray, 0);
            // ...and bind it to our array
            gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesArray[0]);

            // create nearest filtered texture
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

            // Use Android GLUtils to specify a two-dimensional texture image from our bitmap
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

            result = texturesArray[0];
        }

        return result;
    }
}
